<template>
<div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="读者姓名">
            <el-input v-model="dataForm.name" clearable></el-input>
        </el-form-item>
        <el-form-item label="读者编号">
            <el-input v-model="dataForm.id" clearable></el-input>
        </el-form-item>
        <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button type="primary" @click="register()">注册</el-button>
        </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
        <el-table-column prop="id" header-align="center" align="center" label="读者编号" width="120"></el-table-column>
        <el-table-column prop="name" header-align="center" align="center" label="姓名"></el-table-column>
        <el-table-column prop="gender" header-align="center" align="center" label="性别" width="50">
            <template slot-scope="scope">{{scope.row.gender==1?'男':'女'}}</template>
        </el-table-column>
        <el-table-column prop="idNo" header-align="center" align="center" label="身份证号" width="170"></el-table-column>
        <el-table-column header-align="center" align="center" label="出生日期" width="95">
            <template slot-scope="scope">{{scope.row.birthday | formatDate}}</template>
        </el-table-column>
        <el-table-column prop="phone" header-align="center" align="center" label="手机号码"></el-table-column>
        <el-table-column header-align="center" align="center" label="读者证类型">
            <template slot-scope="scope">{{scope.row.type==1?'借阅':scope.row.type==2?'普通外借':'参考外借'}}</template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="注册日期">
            <template slot-scope="scope">{{scope.row.registryDate | formatDate}}</template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="注销日期">
            <template slot-scope="scope">{{scope.row.cancelDate | formatDate}}</template>
        </el-table-column>
        <el-table-column header-align="center" align="center" width="120" label="操作">
            <template slot-scope="scope">
                <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
                <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">注销</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
</div>
</template>

<script>
import AddOrUpdate from "./treader-add-or-update";

export default {
    data() {
        return {
            dataForm: {
                id: "",
                name: ""
            },
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            dataListLoading: false,
            addOrUpdateVisible: false
        };
    },
    components: {
        AddOrUpdate
    },
    activated() {
        this.getDataList();
    },
    methods: {
        // 获取数据列表
        getDataList() {
            this.dataListLoading = true;
            this.$http({
                url: this.$http.adornUrl("/generator/treader/list"),
                method: "get",
                params: this.$http.adornParams({
                    page: this.pageIndex,
                    limit: this.pageSize,
                    name: this.dataForm.name,
                    id: this.dataForm.id
                })
            }).then(({
                data
            }) => {
                if (data && data.code === 0) {
                    this.dataList = data.page.list;
                    this.totalPage = data.page.totalCount;
                } else {
                    this.dataList = [];
                    this.totalPage = 0;
                }
                this.dataListLoading = false;
            });
        },
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val;
            this.pageIndex = 1;
            this.getDataList();
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val;
            this.getDataList();
        },
        register() {
            this.$router.push({
                name: "wenyuan-reader/treader-register"
            });
        },
        // 新增 / 修改
        addOrUpdateHandle(data) {
            this.addOrUpdateVisible = true;
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(data);
            });
        },
        // 删除
        deleteHandle(id) {
            this.$confirm(`确定对[id=${id}]进行注销操作?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.$http({
                    url: this.$http.adornUrl("/generator/treader/delete"),
                    method: "post",
                    data: this.$http.adornData([id], false)
                }).then(({
                    data
                }) => {
                    if (data && data.code === 0) {
                        this.$message({
                            message: "注销成功",
                            type: "success",
                            duration: 1500,
                            onClose: () => {
                                this.getDataList();
                            }
                        });
                    } else {
                        this.$message.error(data.msg);
                    }
                });
            });
        }
    }
};
</script>
